#ifndef LIB_IO_H_
#define LIB_IO_H_
#include "stdint.h"

// 一次写入一个字节
static inline void outb(uint16_t port, uint8_t data) {
    asm volatile ("outb %b0, %w1" : : "a"(data), "Nd"(port));
}
// 一次写入多个字(2字节). 将addr开始word_cnt个字写入port
static inline void outsw(uint16_t port, const void* addr, uint32_t word_cnt) {
    asm volatile ("cld; rep outsw" : "+S"(addr), "+c"(word_cnt) : "d"(port));
}
// 一次读入一个字节
static inline uint8_t inb(uint16_t port) {
    uint8_t data;
    asm volatile ("inb %w1, %b0" : "=a"(data) : "Nd"(port));
    return data;
}
// 一次读入多个字(2字节). 从端口Port读入word_cnt个字, 写入addr
static inline void insw(uint16_t port, void* addr, uint32_t word_cnt) {
    asm volatile ("cld; rep insw" : "+D"(addr), "+c" (word_cnt) : "d"(port) : "memory");
}

#endif  //LIB_IO_H_